// pages/user/company/company.js
const common = require('../../utils/util.js')
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
        userInfo: null,
        companyProperty: {},
        companyData: {},
        companyTrends: {},
        companyUserTrends: {},
        companyTrendsType: 'today',
        companyUserTrendsType: 'today'
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {
        this.getUserInfo()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function() {
        common.refresh()
        setTimeout(function() {
            wx.stopPullDownRefresh()
        }, 2000)
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function() {

    },

    getUserInfo: function() {
        const that = this
        let data = {}
        common.post('User/user_info', data).then(function(res) {
            if (res.code == 204) {
                return
            } else {
                that.setData({
                    userInfo: res
                })
                that.getCompanyProperty()
                that.getCompanyData()
                that.getCompanyTrends()
                that.getCompanyUserTrends()
            }
        }).catch(function(e) {})
    },

    getCompanyProperty: function() {
        const that = this
        let companyProperty = this.data.companyProperty
        let data = {
            client: 2,
            store_id: this.data.userInfo.store_id,
            filiale_id: this.data.userInfo.filiale_id
        }
        common.post('Data/get_money', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    companyProperty: companyProperty
                })
            } else {
                that.setData({
                    companyProperty: res
                })
            }
        }).catch(function(e) {})
    },

    getCompanyData: function() {
        const that = this
        let companyData = this.data.companyData
        let data = {
            client: 2,
            store_id: this.data.userInfo.store_id,
            filiale_id: this.data.userInfo.filiale_id
        }
        common.post('Data/c_get_all_user', data).then(function(res) {
            if (res.code == 204) {
                that.setData({
                    companyData: companyData
                })
            } else {
                that.setData({
                    companyData: res
                })
            }
        }).catch(function(e) {})
    },

    getCompanyTrends: function() {
        const that = this
        let data = {
            client: 2,
            store_id: this.data.userInfo.store_id,
            filiale_id: this.data.userInfo.filiale_id
        }
        common.post('Data/get_user_dynamic', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                companyTrends: res
            })
        }).catch(function(e) {})
    },

    getCompanyUserTrends: function() {
        const that = this
        let data = {
            client: 2,
            store_id: this.data.userInfo.store_id,
            filiale_id: this.data.userInfo.filiale_id
        }
        common.post('Data/get_vip_dynamic', data).then(function(res) {
            if (res.code == 204) {
                return
            }
            that.setData({
                companyUserTrends: res
            })
        }).catch(function(e) {})
    },

    goPropertyDetail: function(e) {
        if (e.currentTarget.dataset.time) {
            wx.navigateTo({
                url: '/pages/company/companyProperty/companyProperty?company=' + e.currentTarget.dataset.company + '&time=' + e.currentTarget.dataset.time
            })
        } else {
            wx.navigateTo({
                url: '/pages/company/companyProperty/companyProperty?company=' + e.currentTarget.dataset.company
            })
        }
    },

    goCompanyData: function(e) {
        wx.navigateTo({
            url: '/pages/company/companyData/companyData?type=' + e.currentTarget.dataset.type + '&company=' + e.currentTarget.dataset.company
        })
    },

    goTrendsMore: function(e) {
        wx.navigateTo({
            url: '/pages/company/companyTrends/companyTrends?company=' + e.currentTarget.dataset.company + '&filiale=' + e.currentTarget.dataset.filiale
        })
    },

    goUserMore: function(e) {
        wx.navigateTo({
            url: '/pages/company/companyUserTrends/companyUserTrends?company=' + e.currentTarget.dataset.company + '&filiale=' + e.currentTarget.dataset.filiale
        })
    },

    trendsChange: function(e) {
        if (e.currentTarget.dataset.type == this.data.companyTrendsType) {
            return
        }
        this.setData({
            companyTrendsType: e.currentTarget.dataset.type
        })
    },

    companyUserTrendsChange: function(e) {
        if (e.currentTarget.dataset.type == this.data.companyUserTrendsType) {
            return
        }
        this.setData({
            companyUserTrendsType: e.currentTarget.dataset.type
        })
    },

    goUserBrowse: function() {
        wx.navigateTo({
            url: '/pages/user/user_browse/user_browse?type=company'
        })
    }
})